package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallAdminMapper;
import org.linlinjava.litemall.db.dao.LitemallStoreMapper;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallAdmin.Column;
import org.linlinjava.litemall.db.domain.LitemallAdminExample;
import org.linlinjava.litemall.db.domain.LitemallStore;
import org.linlinjava.litemall.db.domain.LitemallStoreExample;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class LitemallAdminService {

    private final Column[] result = new Column[]{Column.id, Column.username, Column.avatar, Column.roleIds};
    @Resource
    private LitemallAdminMapper adminMapper;

    @Resource
    private LitemallStoreService storeService;

//    @Lazy
//    @Resource
//    private LitemallStoreMapper storeMapper;

    public List<LitemallAdmin> findAdmin(String username) {
        LitemallAdminExample example = new LitemallAdminExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    public LitemallAdmin findAdmin(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public List<LitemallAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order, String admin) {
        LitemallAdminExample example = new LitemallAdminExample();
        LitemallAdminExample.Criteria criteria = example.createCriteria();
        String superAdmin = "txl";
        if(!admin.equalsIgnoreCase(superAdmin)){
            criteria.andUsernameNotEqualTo(superAdmin);
        }
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public int updateById(LitemallAdmin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallAdmin admin) {
        admin.setAddTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.insertSelective(admin);
    }

    public LitemallAdmin findById(Integer id) {
        return adminMapper.selectByPrimaryKeySelective(id, result);
    }

    public List<LitemallAdmin> all() {
        LitemallAdminExample example = new LitemallAdminExample();
        example.or().andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public Boolean checkUsername(String username, Integer storeId) {
        username = username.toLowerCase();
        List<LitemallAdmin> litemallAdmins = adminMapper.selectByExample(new LitemallAdminExample().createCriteria().andUsernameEqualTo(username).example());
        if (litemallAdmins == null||litemallAdmins.size()==0) {
            return false;
        }

        //判断是否已经绑定
        LitemallStoreExample litemallStoreExample = new LitemallStoreExample();
        LitemallStoreExample.Criteria criteria = litemallStoreExample.createCriteria();
        List<LitemallStore> litemallStores = null;
        if (storeId != null) {
            criteria.andIdEqualTo(storeId);
            criteria.andUsernameEqualTo(username);
            litemallStores = storeService.selectByExample(litemallStoreExample);
            if (litemallStores != null && litemallStores.size() == 1) {
                return true;
            }
        } else {
            criteria.andUsernameEqualTo(username);
            litemallStores = storeService.selectByExample(litemallStoreExample);
            if (litemallStores != null && litemallStores.size() == 0) {
                return true;
            }
        }


        return false;
    }
}
